# -*- coding: utf-8 -*-
from __future__ import print_function
import click
import os
import re
import face_recognition.api as face_recognition
import multiprocessing
import itertools
import sys
import PIL.Image
import numpy as np
import pymysql
import json
import redis


def scan_known_people(known_people_folder):
    known_names = []
    known_face_encodings = []

    for file in image_files_in_folder(known_people_folder):
        basename = os.path.splitext(os.path.basename(file))[0]
        img = face_recognition.load_image_file(file)
        encodings = face_recognition.face_encodings(img)

        if len(encodings) > 1:
            click.echo("WARNING: More than one face found in {}. Only considering the first face.".format(file))

        if len(encodings) == 0:
            click.echo("WARNING: No faces found in {}. Ignoring file.".format(file))
        else:
            known_names.append(basename)
            known_face_encodings.append(encodings[0].tolist())
            
   
    known_names_encode = json.dumps(known_names)
    known_face_encodings_ecode = json.dumps(known_face_encodings)
    # print(known_names_encode)
    # print(known_face_encodings_ecode)
    # conn=pymysql.connect(
    #     host='127.0.0.1',
    #     port=3306,
    #     user='root',
    #     password='root',
    #     db='face_recognition',
    #     charset='utf8'
    # )
         
    #     # 获取游标
    # cursor=conn.cursor()
         
    #     # 执行sql语句
    # sql='insert into white_face(id,name,arr) values(%s,%s,%s)'
    # rows=cursor.execute(sql,('',known_names_encode,known_face_encodings_ecode))
         
    #     # 提交
    # conn.commit()
         
    #     # 关闭游标
    # cursor.close()
         
    #     # 关闭连接
    # conn.close()

    return known_names_encode, known_face_encodings_ecode

def image_files_in_folder(folder):
    return [os.path.join(folder, f) for f in os.listdir(folder) if re.match(r'.*\.(jpg|jpeg|png)', f, flags=re.I)]
def search_white_face():

    conn=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='root',
        db='face_recognition',
        charset='utf8'
    )
         
        # 获取游标
    cursor=conn.cursor()
         
        # 执行sql语句
    sql='select * from white_face where id=1'
    rows=cursor.execute(sql)
         
    arr = cursor.fetchone()
    print(arr[1])
def test_redis(known_names_encode,known_face_encodings_ecode):
    r = redis.Redis(host='127.0.0.1',port=6379,decode_responses=True)
    r.set('site','www.qweqwe.cn')
    r.set('name',known_names_encode)
    r.set('known_names_encode',known_face_encodings_ecode)
    # print(known_names_encode)
    # print(known_face_encodings_ecode)
@click.command()
@click.argument('known_people_folder')
def main(known_people_folder):
    known_names_encode, known_face_encodings_ecode = scan_known_people(known_people_folder)
    # search_white_face()
    test_redis(known_names_encode,known_face_encodings_ecode)
    # print(known_names)
if __name__ == "__main__":
    main()
